package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.PopupAnnouncementDao;
import com.sc.nft.entity.PopupAnnouncement;
import com.sc.nft.entity.dto.PopupAnnouncementAdminAddDTO;
import com.sc.nft.entity.dto.PopupAnnouncementAdminModificationDTO;
import com.sc.nft.entity.vo.PopupAnnouncementAdminVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.PopupAnnouncementService;
import com.sc.nft.sup.PageRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * app维护以及维护完成的弹窗表(PopupAnnouncement)表服务实现类
 *
 * @author wwc
 * @since 2023-07-05 16:58:05
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class PopupAnnouncementServiceImpl implements PopupAnnouncementService {

    private final PopupAnnouncementDao popupAnnouncementDao;

    @Override
    public IPage<PopupAnnouncementAdminVO> getPage(PageRequest pageRequest) {
        LambdaQueryWrapper<PopupAnnouncement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PopupAnnouncement::getPopupType, 2);
        Page<PopupAnnouncement> page = popupAnnouncementDao.page(pageRequest.toMp(), queryWrapper);
        IPage<PopupAnnouncementAdminVO> convert = page.convert(popupAnnouncement -> {
            PopupAnnouncementAdminVO popupAnnouncementAdminVO = new PopupAnnouncementAdminVO();
            BeanUtil.copyProperties(popupAnnouncement, popupAnnouncementAdminVO);
            return popupAnnouncementAdminVO;
        });
        return convert;
    }

    @Override
    public void removeById(Long id) {
        if (id == 1L) {
            throw new GlobalRunTimeException("该数据不可删除");
        }
        popupAnnouncementDao.removeById(id);
    }

    @Override
    public void modification(PopupAnnouncementAdminModificationDTO popupAnnouncementAdminModificationDTO) {
        if (popupAnnouncementAdminModificationDTO.getPopupType().equals(1)) {
            throw new GlobalRunTimeException("该状态禁用");
        }
        PopupAnnouncement popupAnnouncement = popupAnnouncementDao.getById(popupAnnouncementAdminModificationDTO.getId());
        if (BeanUtil.isEmpty(popupAnnouncement)) {
            throw new GlobalRunTimeException("该版本不存在");
        }
        BeanUtil.copyProperties(popupAnnouncementAdminModificationDTO, popupAnnouncement);
        popupAnnouncementDao.updateById(popupAnnouncement);
    }

    @Override
    public void add(PopupAnnouncementAdminAddDTO popupAnnouncementAdminAddDTO) {
        if (popupAnnouncementAdminAddDTO.getPopupType().equals(1)) {
            throw new GlobalRunTimeException("该状态禁用");
        }
        PopupAnnouncement popupAnnouncement = popupAnnouncementDao.getByAppVersion(popupAnnouncementAdminAddDTO.getAppVersion());
        if (BeanUtil.isNotEmpty(popupAnnouncement)) {
            // 当前时间大于等于开始时间，小于等于结束时间
            if (popupAnnouncementAdminAddDTO.getStartTime().getTime() >= popupAnnouncement.getStartTime().getTime() && popupAnnouncementAdminAddDTO.getStartTime().getTime() <= popupAnnouncement.getEndTime().getTime()) {
                throw new GlobalRunTimeException("该版本已存在");
            }
            // 开始时间不能大于结束时间
            if (popupAnnouncementAdminAddDTO.getStartTime().getTime() > popupAnnouncement.getEndTime().getTime()) {
                throw new GlobalRunTimeException("开始时间不能大于结束时间");
            }
        }
        PopupAnnouncement save = new PopupAnnouncement();
        BeanUtil.copyProperties(popupAnnouncementAdminAddDTO, save);
        popupAnnouncementDao.save(save);
    }

}